<?php

/*
 * 权限控制器
 * @author turtle
 * create_time 2017-01-02
 */
 

namespace App\Admin\Controllers\Roles;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Constraint;
use Intervention\Image\Facades\Image;
use App\Admin\Controllers\Controller;
use App\Admin\Models\Role;
use App\Admin\Models\Permission;
use App\Admin\Models\PermissionRole;
use App\Admin\Models\Logs;
use App\Admin\Models\User;

class RoleController extends Controller {

    /*
     * 角色列表页
     * @author turtle
     * create_time 2017-01-02
     */

    public function index() {
        $bool = User::checkUserPermissions('check_role');
        if (!$bool) {
            return redirect()
                ->route('admin.dashboard')
                ->with([ 
                    'message' => "您没有拥有该权限，请联系管理员",
                    'alert-type' => 'error',
                ]);
        }
        $edit_role = User::checkUserPermissions('edit_role');
        $del_role = User::checkUserPermissions('del_role');
        $add_role = User::checkUserPermissions('add_role');
        if (Auth::user()->role_id == 1) {
            $roles = Role::get();
        } else {
            $roles = Role::where('id', '<>', '1')->get();
        }
        $datas['edit_role'] = $edit_role;
        $datas['del_role'] = $del_role;
        $datas['roles'] = $roles;
        $datas['add_role'] = $add_role;
        return view('admin.role', $datas);
    }

    /*
     * 添加/编辑角色页面
     * @author turtle
     * create_time 2017-01-02
     */

    public function add($id=null) {
        if (empty($id)) {
            $bool = User::checkUserPermissions('add_role');
        } else {
            $bool = User::checkUserPermissions('edit_role');
        }
        if (!$bool) {
            return redirect()
                ->route('admin.dashboard')
                ->with([ 
                    'message' => "您没有拥有该权限，请联系管理员",
                    'alert-type' => 'error',
                ]);
        }
        $id = intval($id);
        if (!empty($id)) {
            $role = Role::find($id);
            $selectedPermission = PermissionRole::where('role_id', $id)->select('permission_id')->get();
            foreach ($selectedPermission as $key=>$permission) {
                $selectedPermissions[$key] = $permission->permission_id;
            }
            $datas['role'] = $role;
            $datas['selectedPermission'] = $selectedPermissions;
        }

        $permissions = Permission::getAllPermissions();

        $datas['permissions'] =  $permissions;
        $datas['id'] = $id;

        return  view('admin.edit_role', $datas);
    }

    /*
     * 添加/编辑角色
     * @author turtle
     * create_time 2017-01-02
     */

    public function postAdd(Request $request) {
        $role['name'] = $request->input('name');
        $role['display_name'] = $request->input('display_name');
        $permissions = $request->input('permissions');
        if (count($permissions) == 0 ) {
            return redirect()
                ->route('roles.add')
                ->with([
                    'message' => "请选择权限",
                    'alert-type' => 'error',
                ]);

        }
        $role_id = $request->input('role_id');
        if (empty($role_id)) {
            $bool = User::checkUserPermissions('add_role');
            if (!$bool) {
                return redirect()
                    ->route('admin.dashboard')
                    ->with([ 
                        'message' => "您没有拥有该权限，请联系管理员",
                        'alert-type' => 'error',
                    ]);
            }
            $role_id = Role::addRole($role);

            $rs = PermissionRole::addPermissionRole($permissions, $role_id);
            if ($rs) {
                $log = "添加角色".$role['name']."成功";
                Logs::addLog($log);
                return redirect()
                    ->route('role.roles')
                    ->with([
                        'message' => " 添加成功",
                        'alert-type' => 'success',
                    ]);
            }  else {
                $log = "添加角色".$role['name']."失败";
                Logs::addLog($log);
                return redirect()
                    ->route('role.roles')
                    ->with([
                        'message' => " 添加失败",
                        'alert-type' => 'error',
                    ]);

            }

        } else {
            $bool = User::checkUserPermissions('edit_role');
            if (!$bool) {
                return redirect()
                    ->route('admin.dashboard')
                    ->with([ 
                        'message' => "您没有拥有该权限，请联系管理员",
                        'alert-type' => 'error',
                    ]);
            }
            Role::editRole($role, $role_id);
            $rs = PermissionRole::editPermissionRole($permissions, $role_id);
            if ($rs) {
                $log = "编辑角色".$role['name']."成功";
                Logs::addLog($log);
                return redirect()
                    ->route('role.roles')
                    ->with([
                        'message' => " 编辑成功",
                        'alert-type' => 'success',
                    ]);
            } else {
                $log = "编辑角色".$role['name']."失败";
                Logs::addLog($log);
                return redirect()
                    ->route('role.roles')
                    ->with([
                        'message' => " 编辑失败",
                        'alert-type' => 'error',
                    ]);

            }
        }


    }

    /*
     * 查看角色详情
     * @author turtle
     * create_time 2017-01-02
     */

    public function view($id) {
        $id = intval($id);

        $role = Role::find($id);
        if (empty($role)) {
            return redirect()
                ->route('role.roles')
                ->with([
                    'message' => "抱歉，该角色不存在",
                    'alert-type' => 'error',
                ]);

        }
        $edit_role = User::checkUserPermissions('edit_role');
        $del_role = User::checkUserPermissions('del_role');

        $permissions = PermissionRole::getPermissionInfo($id);
        $permissionInfo = Permission::getPermissionInfo($permissions);

        $datas['role'] = $role;
        $datas['permissions'] = $permissionInfo;
        $datas['edit_role'] = $edit_role;
        $datas['del_role'] = $del_role;
        return view('admin.view_role', $datas);
    }

    /*
     * 删除角色
     * @author turtle
     * create_time 2017-01-03
     */

    public function deleteRole($id) {
        $bool = User::checkUserPermissions('del_role');
        if (!$bool) {
            return redirect()
                ->route('admin.dashboard')
                ->with([ 
                    'message' => "您没有拥有该权限，请联系管理员",
                    'alert-type' => 'error',
                ]);
        }
        $id = intval($id);
        $role = Role::find($id);
        if (empty($role)) {
            return redirect()
                ->route('role.roles')
                ->with([
                    'message' => "抱歉，该角色不存在",
                    'alert-type' => 'error',
                ]);

        }
        Role::where('id', $id)->delete();
        $rs = PermissionRole::where('role_id', $id)->delete();
        if ($rs) {
            $log = "删除角色".$role->name."成功";
            Logs::addLog($log);
            return redirect()
                ->route('role.roles')
                ->with([
                    'message' => "删除成功",
                    'alert-type' => 'success',
                ]);

        } else {
            $log = "删除角色".$role->name."失败";
            Logs::addLog($log);
            return redirect()
                ->route('role.roles')
                ->with([
                    'message' => "删除失败",
                    'alert-type' => 'error',
                ]);

        }
    }
}
